<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #ball{width: 100px;height: 100px;background-color: red;border-radius:50%;margin-left: 0px;}
        #ball2{width: 100px;height: 100px;background-color: red;border-radius:50%;margin-left: 0px;}
    </style>
</head>
<body>
<div id="ball"></div>
<div id="ball2"></div>
<input type="button" value="球1" onclick="move1('ball')">
<input type="button" value="球2" onclick="move2('ball2')">
<script>
    var ball = document.getElementById('ball');
    var ball2 = document.getElementById('ball');
    var move1 = go();
    var move2 = go();
    function go(){
        var t;//闭包
        function myMove(id){
            var htmlObj = document.getElementById(id);
            clearInterval(t);
            var end = 800;
            var offset = 2;     //速度
            function move(){
                var cssObj = getComputedStyle(htmlObj);
                var originL = parseInt(cssObj.marginLeft);
                if (originL>=end){
                    clearInterval(t);
                } else {
                    htmlObj.style.marginLeft = originL + offset + 'px';
                }
            }
            t = setInterval(move,20);//1
        }
        return myMove;
    }
</script>
</body>
</html>